﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace CSMF
{
    public partial class VipRegist : System.Web.UI.Page
    {

        

        private UserRegistrationService _registrationService = new UserRegistrationService();
        private AuthenticationService _authenticationService = new AuthenticationService();


        protected void Page_Load(object sender, EventArgs e)
        {
            PasswordText.Attributes.Add("value", PasswordText.Text);

            if(!IsPostBack)
            {
               _authenticationService.SignOut();
            }
        }

        //private string GetUserLogo(string imgUrl)
        //{
        //    var picName= imgUrl.Substring(imgUrl.LastIndexOf("/"));
        //    return "/Upload/UserLogo/" + picName;
        //}


        protected void Reg_Click(object sender, EventArgs e)
        {
            msgLbl.Visible = false;
            if (string.IsNullOrEmpty(UserLogoUrl.Value))
            {
                msgLbl.Text = "请上传头像";
                msgLbl.Visible = true;
                return;
            }
            if (string.IsNullOrEmpty(Intro.Text))
            {
                msgLbl.Text = "请填写个人简介";
                msgLbl.Visible = true;
                return;
            }


            var user = GetGuestCustomer();


            var request = new UserRegistrationRequest()
            {
                Customer = user,
                Email = this.EmailText.Text,
                Username = this.UserNameText.Text,
                IsApproved = true,
                Password = this.PasswordText.Text,
                IsVip = true,
                Intro = this.Intro.Text,
                UserLogo = UserLogoService.GetUserLogo(this.UserLogoUrl.Value)

            };
            var response = _registrationService.RegisterCustomer(request);
            if (response.Success)
            {
                _authenticationService.SignIn(request.Customer, true);
                Response.Redirect("VipBindSina.aspx");

            }
            else
            {
                var error = response.Errors;
                msgLbl.Text = "注册失败: " + error[0];
            }
        }

       
        private UserInfo GetGuestCustomer()
        {
            var _workContext = new WebWorkContext();

            if (_workContext.CurrentUser.IsRegistered())
            {
                //Already registered customer. 
                _authenticationService.SignOut();

                //Save a new record
                var userSerivce = new UserService();
                //Save a new record
                _workContext.CurrentUser= userSerivce.InsertGuestCustomer();
            }

            var user = _workContext.CurrentUser;
            return user;
        }

        protected void btnUp_Click(object sender, EventArgs e)
        {
            if (this.fuInsertLogo.PostedFile.FileName != "")
            {
                var picUrl = UserLogoService.FileUploads(fuInsertLogo, "..\\..\\UpLoad\\UserLogo\\");

               // var picUrl = FileUploads(this, fuInsertLogo, "..\\..\\UpLoad\\UserLogo\\");
                lblIMessage.Text = "图片上传成功！";
                this.imgInsertLogo.ImageUrl = picUrl;
                this.UserLogoUrl.Value = picUrl;
            }
            else
            {
                lblIMessage.Text = "请选择要上传的图片！";
            }
        }


        //public static string FileUploads(Page page, FileUpload fileProduct, string url)
        //{
        //    if (!fileProduct.HasFile)
        //    {
        //        return "出错";

        //    }
        //    try
        //    {
        //        string urlimg = DateTime.Now.ToString("hhmmss") + fileProduct.FileName;
        //        var picPath = ConfigurationManager.AppSettings["PicSavePath"] + urlimg;
        //        fileProduct.SaveAs(picPath);
        //        var picUrl = ConfigurationManager.AppSettings["PicDomain"] + urlimg;
        //        return picUrl;
        //    }
        //    catch
        //    {
        //        return "出错";
        //    }
        //}
    }
}